package br.com.fiap.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.fiap.ConnectionFactory.ConnectionFactory;
import br.com.fiap.bean.Banco;

public class BancoDAO 
{

	
	public ArrayList<Banco> getAll() throws SQLException, ClassNotFoundException{
		
		ArrayList<Banco> bancos = new ArrayList<Banco>();
		
		Connection conn = ConnectionFactory.getConnection();
		
		String sql = "SELECT id, nome FROM BANCO.BANCO ORDER BY id";
		
		PreparedStatement stmt = conn.prepareStatement(sql);
		
		ResultSet rs = stmt.executeQuery();
		
		while(rs.next()){
			
			Banco b = new Banco();			
			b.setNumero(rs.getInt("id"));
			b.setNome(rs.getString("nome"));
			
			bancos.add(b);
		}

		conn.close();
		
		return bancos;
	}
	
	
		public Banco getUnique(int numero) throws SQLException, ClassNotFoundException{
			
			Banco banco = new Banco();
			
			Connection conn = ConnectionFactory.getConnection();
			
			String sql = "SELECT id, nome FROM BANCO.BANCO WHERE id = ?";
			
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setInt(1, numero);
			
			ResultSet rs = stmt.executeQuery();
			
			if(rs.next()){
				
				banco.setNumero(rs.getInt("id"));
				banco.setNome(rs.getString("nome"));
			}

			conn.close();
			
			return banco;
		}
		
	}


